package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.common.bean.PageParamVo;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.mapper.SkuBoundsMapper;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import com.atguigu.gmall.sms.service.SkuFullReductionService;
import com.atguigu.gmall.sms.service.SkuLadderService;
import com.atguigu.gmall.sms.vo.SkuSaleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBoundsEntity> implements SkuBoundsService {


    @Autowired
    private SkuFullReductionService skuFullReductionService;

    @Autowired
    private SkuBoundsService skuBoundsService;

    @Autowired
    private SkuLadderService skuLadderService;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuBoundsEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageResultVo(page);
    }
    //向sms表中插入数据
    @Override
    @Transactional
    public void saveSkuSaleInfo(SkuSaleVo skuSaleVo) {
        // 3. 保存营销相关信息，需要远程调用gmall-sms
        // 3.1. 积分优惠
        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        BeanUtils.copyProperties(skuSaleVo, skuBoundsEntity);
        //处理work字段 list 转换为 int
        List<Integer> work = skuSaleVo.getWork();
        if (!Collections.isEmpty(work) && work.size() == 4){
            int i = work.get(0) * 8 + work.get(1) * 4 + work.get(2) * 2 + work.get(3);
            skuBoundsEntity.setWork(i);
        }
        this.skuBoundsService.save(skuBoundsEntity);
        // 3.2. 满减优惠
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuSaleVo,skuFullReductionEntity);
        //字段属性不同 做出更改
        skuFullReductionEntity.setAddOther(skuSaleVo.getFullAddOther());
        this.skuFullReductionService.save(skuFullReductionEntity);
        // 3.3. 数量折扣
        //创建对象
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        //赋值
        BeanUtils.copyProperties(skuSaleVo, skuLadderEntity);
        //保存
        skuLadderEntity.setAddOther(skuSaleVo.getLadderAddOther());
        skuLadderService.save(skuLadderEntity);
    }

}